import { compare, isInArray } from "@/utils"
import { MissonModel } from "../room/Misson/missonModel"
/*  powerspawn任务 */
export default class PowerSpawnExtension extends StructurePowerSpawn {
    public HandMisson():void{
        if (!this.room.memory.Task['STRUCTURE']) this.room.memory.Task['STRUCTURE'] = []
        var allmyTask = []
        for (var task of this.room.memory.Task['STRUCTURE'])
        {
            if (isInArray(task.relateStructure,this.id))
            {
                allmyTask.push(task)
            }
        }
        /* 按照优先级排序 */
        if (allmyTask.length <= 0) return
        else if (allmyTask.length > 1)
            allmyTask.sort(compare('taskLevel'))
        /* 处理任务 */
        let thisTask = allmyTask[0]
        if (thisTask.delayTick < 99995)
            thisTask.delayTick--
        switch (thisTask.Name){
            case "power升级":{this.HandlePU()}
        }
    }

    public HandlePU():void{
        var thisTask = this.room.getMisson('STRUCTURE','power升级')
        if (thisTask.state == 0)
        {
            if (Game.time % 16) return
            var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
            if (!storage_ || storage_.store['power'] < 1000 || storage_.store['energy'] < 10000) return
            if (!this.store['power'] || this.store['power'] < 100)
            {
                if (!this.room.getMisson_S("CREEP",'物流搬运','manage01'))
                {
                    var TaskCreep:RoomTask = {} as RoomTask
                    Object.assign(TaskCreep,MissonModel['物流搬运'])
                    TaskCreep.relateCreep = ['manage01']
                    TaskCreep.disnum = 100
                    TaskCreep.relateResource = ['power']
                    TaskCreep.sourceStructure = [storage_.id]
                    TaskCreep.taskLevel = 2
                    TaskCreep.disStructure = [this.id]
                    this.room.mountMisson(TaskCreep)
                }
                return
            }
            if(!this.store['energy'] || this.store['energy'] < 5000)
            {
                if (!this.room.getMisson_S("CREEP",'物流搬运','manage01'))
                {
                    var TaskCreep:RoomTask = {} as RoomTask
                    Object.assign(TaskCreep,MissonModel['物流搬运'])
                    TaskCreep.relateCreep = ['manage01']
                    TaskCreep.disnum = 5000
                    TaskCreep.relateResource = ['energy']
                    TaskCreep.sourceStructure = [storage_.id]
                    TaskCreep.taskLevel = 2
                    TaskCreep.disStructure = [this.id]
                    this.room.mountMisson(TaskCreep)
                }
                return
            }
            thisTask.state = 1
        }
        else if (thisTask.state == 1)
        {
            /* 实施冲级任务 */
            var result = this.processPower()
            if (result != 0)
            {
                thisTask.state = 0
            }
        }
    }
}